package cn.cycleif.admin.service;

import cn.cycleif.admin.pojo.dto.AssignRoleMenuDTO;
import cn.cycleif.app.mapper.MenuMapper;
import cn.cycleif.app.mapper.RoleMenuMapper;
import cn.cycleif.entity.Menu;
import cn.cycleif.entity.RoleMenu;
import cn.cycleif.util.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Date: 25/2/6-17:18
 * @Desc:
 */
@Service
public class AdminMenuService extends ServiceImpl<MenuMapper, Menu> {

  @Autowired
  private MenuMapper menuMapper;
  @Autowired
  private RoleMenuMapper roleMenuMapper;

  public R<String> add(Menu menu) {
    menuMapper.insert(menu);
    return R.success();
  }

  public R<String> assign(AssignRoleMenuDTO dto) {
    Integer roleId = dto.getRoleId();
    List<Integer> menuIds = dto.getMenuIds();
    for (Integer menuId : menuIds) {
      RoleMenu roleMenu = new RoleMenu();
      roleMenu.setRoleId(roleId);
      roleMenu.setMenuId(menuId);
      roleMenuMapper.insert(roleMenu);
    }
    return R.success();
  }
}
